//
//  HistoryViewController.swift
//  FootballDataRecord
//
//  Created by Xx Jiang on 2020/1/9.
//  Copyright © 2020 Xx Jiang. All rights reserved.
//

import UIKit

class HistoryViewController: BaseViewController,UITableViewDelegate,UITableViewDataSource {
    var AllMatch =  MatchModel.getAllMatch()
    
    @IBOutlet weak var tableview: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "历史记录"
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
        
        setUI()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        AllMatch = MatchModel.getAllMatch()
        tableview.reloadData()
    }
    func setUI()  {
        self.tableview.delegate = self
        self.tableview.dataSource = self
        self.tableview.register(UINib.init(nibName: "HistoryMatchTableViewCell", bundle: nil), forCellReuseIdentifier: "HistoryMatchTableViewCell")
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return AllMatch.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "HistoryMatchTableViewCell") as! HistoryMatchTableViewCell
        cell.selectionStyle = .none
        let model = AllMatch[indexPath.row]
        if !(DYStringIsEmpty(value: model.matchTeamA as AnyObject?) ||  DYStringIsEmpty(value: model.matchTeamB as AnyObject?)) {
         cell.setcell(model: AllMatch[indexPath.row])
        }
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 136.0
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        
        let matchModel = AllMatch[indexPath.row]
        if matchModel.matchStatus == "0" || matchModel.matchStatus == nil {
            if DYStringIsEmpty(value: matchModel.matchTeamA as AnyObject?) ||  DYStringIsEmpty(value: matchModel.matchTeamB as AnyObject?) {
                let vc = CreateGameViewController()
                vc.matchModel = matchModel
                navigationController?.pushViewController(vc, animated: true)
            }else{
                let vc = AddGameDetailHomeViewController()
                vc.matchModel = matchModel
                navigationController?.pushViewController(vc, animated: true)
            }
        }else{
            let vc = HistoryMatchDetailHomeViewController()
            vc.matchModel = matchModel
            navigationController?.pushViewController(vc, animated: true)
        }
        
    }
    
}
